package stanPaysNg

import (
	"fmt"
	"strings"
)

type RequestSign interface {
	GetSign(key string) (string, error)
}
type PaymentRequest struct {
	MchId       string `json:"mchId"`
	MchOrderNum string `json:"mchOrderNum"`
	Method      string `json:"method"`
	Amount      string `json:"amount"`
	NotifyUrl   string `json:"notifyUrl"`
	RedirectUrl string `json:"redirectUrl"`
	Description string `json:"description"`
	CustId      string `json:"custId"`
	CustName    string `json:"custName"`
	CustEmail   string `json:"custEmail"`
	CustPhone   string `json:"custPhone"`
	Sign        string `json:"sign"`
}

func (p *PaymentRequest) GetSign(key string) (string, error) {
	// 排序key值并拼接value
	values := []string{p.Amount, p.CustEmail, p.CustId, p.CustName, p.CustPhone, p.Description, p.MchId, p.MchOrderNum, p.Method, p.NotifyUrl, p.RedirectUrl}
	//sort.Strings(values)
	toSignStr := strings.Join(values, "")
	return signData(toSignStr, key)
}

// 回调

type CallbackRequest struct {
	PlatOrderNum string `json:"platOrderNum"`
	Amount       string `json:"amount"`
	MchId        string `json:"mchId"`
	NotifyDesc   string `json:"notifyDesc"`
	Method       string `json:"method"`
	Fee          string `json:"fee"`
	Sign         string `json:"sign"`
	MchOrderNum  string `json:"mchOrderNum"`
	PayData      string `json:"payData"`
	NotifyCode   string `json:"notifyCode"`
	Description  string `json:"description"`
}

func (c *CallbackRequest) verifySign(key string) (bool, error) {
	values := []string{c.Amount, c.Fee, c.MchId, c.MchOrderNum, c.Method, c.NotifyCode, c.NotifyDesc, c.PayData, c.PlatOrderNum}
	toSignStr := strings.Join(values, "")
	//toSignStr = `6666.00266.64M240624002RC20240702064952547763VASUCCESSPayment Successhttps://cashier.stanpays-ng.com/SP1808030318238765056SP1808030318238765056`
	return verifySign(toSignStr, c.Sign, key)
}

// 代付

type DisbursementRequest struct {
	MchId       string `json:"mchId"`       //商户ID
	MchOrderNum string `json:"mchOrderNum"` //商户订单号
	Amount      int64  `json:"amount"`      //代付金额(不支持小数点)
	Name        string `json:"name"`        //收款人名字
	Account     string `json:"account"`     //收款人账号
	Method      string `json:"method"`      //代付方式
	BankCode    string `json:"bankCode"`    //银行编号
	NotifyUrl   string `json:"notifyUrl"`   //异步通知地址
	Sign        string `json:"sign"`
}

func (d *DisbursementRequest) GetSign(key string) (string, error) {
	// 排序key值并拼接value
	values := []string{d.Account, fmt.Sprintf("%d", d.Amount), d.BankCode, d.MchId, d.MchOrderNum, d.Method, d.Name, d.NotifyUrl}
	toSignStr := strings.Join(values, "")
	return signData(toSignStr, key)
}

// 代付一步回调

type DisbursementCallbackRequest struct {
	NotifyCode   string `json:"notifyCode"`   //	通知状态
	NotifyDesc   string `json:"notifyDesc"`   //	通知状态描述(代付失败原因)
	MchId        string `json:"mchId"`        //	商户ID
	MchOrderNum  string `json:"mchOrderNum"`  //	商户订单号
	PlatOrderNum string `json:"platOrderNum"` //	平台订单号
	Amount       string `json:"amount"`       //	支付金额
	Fee          string `json:"fee"`          //	手续费
	Name         string `json:"name"`         //	收款人姓名
	Account      string `json:"account"`      //	收款人账号
	Method       string `json:"method"`       //	代付方式
	BankCode     string `json:"bankCode"`     //	银行编号
	Description  string `json:"description"`  //	描述
	Sign         string `json:"sign"`         //	平台加密字符串
}

func (c *DisbursementCallbackRequest) verifySign(key string) (bool, error) {
	values := []string{c.Account, c.Amount, c.BankCode,
		c.Description, c.Fee, c.MchId, c.MchOrderNum,
		c.Method, c.Name, c.NotifyCode, c.NotifyDesc, c.PlatOrderNum}
	toSignStr := strings.Join(values, "")
	return verifySign(toSignStr, c.Sign, key)
}

// 余额

type BalanceRequest struct {
	MchId    string `json:"mchId"`    //	商户ID
	DateTime string `json:"dateTime"` //	时间字符串（格式:yyyyMMddHHmmss）
	Sign     string `json:"sign"`     //	加密字符串
}

func (b *BalanceRequest) GetSign(key string) (string, error) {
	values := []string{b.DateTime, b.MchId}
	toSignStr := strings.Join(values, "")
	return signData(toSignStr, key)
}
